package com.cocolover2.andbase.mvpbase;


import android.app.Application;
import android.content.Context;
import android.support.v4.util.LruCache;
import android.util.Log;

import com.cocolover2.andbase.cache.DiskLruCacheHelper;
import com.cocolover2.andbase.utils.SPUtils;

import java.io.IOException;
import java.io.Serializable;

/**
 * <p>MVP模式中基本的model</p>
 * 1.sharedPrefrenced轻量持久化保存数据<br>
 * 2.本地磁盘保存{@link DiskLruCacheHelper}<br>
 * 3.内存缓存{@link LruCache}<br>
 *
 * @param <T> 数据对象或者数据集合
 * @since 1.1.1
 */
public abstract class AbBaseModel<T> implements IBaseModel<T> {

    private DiskLruCacheHelper mDiskLruCacheHelper;
    //内存缓存
    private static LruCache<String, String> mLruCache;
    /**
     * 建议使用ApplicationContext
     */
    private Context mContext;

    public AbBaseModel(Context context, String cacheDirName, int maxSize) {
        if (!(context instanceof Application))
            throw new IllegalArgumentException("context must be application");
        this.mContext = context;
        try {
            mDiskLruCacheHelper = new DiskLruCacheHelper(context, cacheDirName);
            mDiskLruCacheHelper.setMaxSize(maxSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (mLruCache == null)
            mLruCache = new LruCache<String, String>((int) (Runtime.getRuntime().maxMemory() / 8));
    }


    @Override
    public void saveToDiskCache(String key, T datas) {
        if (mDiskLruCacheHelper == null) {
            return;
        }
        if (!mDiskLruCacheHelper.isClosed()) {
            if (datas instanceof String)
                mDiskLruCacheHelper.put(key, datas.toString());
            else if (datas instanceof Serializable)
                mDiskLruCacheHelper.put(key, (Serializable) datas);
        } else {
            Log.e("DiskCache", "save diskcache is fail because cache is closed");
        }
    }

    @Override
    public String getFromDiskCache(String key) {
        if (mDiskLruCacheHelper != null && mDiskLruCacheHelper.getAsString(key) != null)
            return mDiskLruCacheHelper.getAsString(key);
        else
            return "";
    }

    @Override
    public Serializable getFromDiskCache2(String key) {
        if (mDiskLruCacheHelper != null)
            return mDiskLruCacheHelper.getAsSerializable(key);
        else
            return null;
    }


    @Override
    public void saveToSPs(String key, Object datas) {
        SPUtils.put(mContext.getApplicationContext(), key, datas);
    }

    @Override
    public Object getFromSPs(String key, Object defaultValue) {
        return SPUtils.get(mContext.getApplicationContext(), key, defaultValue);
    }

    /**
     * 关闭磁盘缓存
     */
    public void closeCache() {
        try {
            if (mDiskLruCacheHelper != null)
                mDiskLruCacheHelper.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void saveToCacheMemory(String key, String data) {
        mLruCache.put(key, data);
    }

    @Override
    public String getFromCacheMemory(String key) {
        return mLruCache.get(key);
    }

    /**
     * 清除内存缓存
     */
    public static void clearCacheMemory() {
        if (mLruCache != null)
            mLruCache.evictAll();
    }

    /**
     * 根据key值清除内存缓存
     */
    public static void removeCacheMemory(String key) {
        if (mLruCache != null)
            mLruCache.remove(key);
    }
}
